/**
 * Oct 11, 2013 10:36:17 AM
 * com.kratonsolution.belian.security.impl.dao
 * UserRole.java
 */
package com.kratonsolution.belian.security.impl.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.kratonsolution.belian.common.util.Numbers;
import com.kratonsolution.belian.common.util.Paging;
import com.kratonsolution.belian.security.api.dao.SecurityDao;
import com.kratonsolution.belian.security.api.dao.UserRoleDao;
import com.kratonsolution.belian.security.api.dm.UserRole;

/**
 * @author Agung Dodi Perdana
 * @email agung.dodi.perdana@gmail.com
 * @Version Sinch 0.0.1
 */
@Component
public class UserRoleDaoImpl extends SecurityDao implements UserRoleDao
{
    public UserRole store(UserRole model)
    {
        getSession().persist(model);
        return model;
    }

    public UserRole load(String id)
    {
        return (UserRole)getSession().get(UserRole.class,id);
    }

    public UserRole update(UserRole model)
    {
        getSession().merge(model);
        return model;
    }

    public UserRole remove(UserRole model)
    {
        getSession().delete(model);
        return model;
    }

    public List<UserRole> loads()
    {
        Query query = getSession().createQuery("FROM UserRole");
        query.setCacheable(true);

        return query.list();
    }

    public List<UserRole> loads(Map<String, Object> param)
    {
        if(param == null || param.isEmpty())
            return loads();
        
        StringBuilder builder = new StringBuilder();
        builder.append("FROM UserRole user WHERE user.id IS NOT NULL ");
        
        if(param.containsKey("email"))
            builder.append("AND user.userEmail = :email ");
        
        if(param.containsKey("role"))
            builder.append("AND user.roleId = :role ");
        
        if(param.containsKey("active"))
            builder.append("AND user.active = :active ");
                
        Query query = getSession().createQuery(builder.toString());
        query.setCacheable(true);
        query.setFirstResult(Paging.getStart(param.get("start")));
        query.setMaxResults(Paging.getRowPerPage(param.get("max")));
        
        if(param.containsKey("email"))
            query.setParameter("email",param.get("email"));
        
        if(param.containsKey("role"))
            query.setParameter("role", param.get("role"));
        
        return query.list();
    }

    public long count()
    {
        Query query = getSession().createQuery("SELECT COUNT(role) FROM UserRole role");
        query.setCacheable(true);
        
        return Numbers.toLong(query.uniqueResult());
    }

    public long count(Map<String, Object> param)
    {
        if(param == null || param.isEmpty())
            return count();
        
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT COUNT(user) FROM UserRole user WHERE user.id IS NOT NULL ");
        
        if(param.containsKey("email"))
            builder.append("AND user.userEmail = :email ");
        
        if(param.containsKey("role"))
            builder.append("AND user.roleId = :role ");
        
        if(param.containsKey("active"))
            builder.append("AND user.active = :active ");
                
        Query query = getSession().createQuery(builder.toString());
        query.setCacheable(true);
        
        if(param.containsKey("email"))
            query.setParameter("email",param.get("email"));
        
        if(param.containsKey("role"))
            query.setParameter("role", param.get("role"));
        
        return Numbers.toLong(query.uniqueResult());
    }

    public List<UserRole> loads(String email)
    {
        StringBuilder builder = new StringBuilder();
        builder.append("FROM UserRole user WHERE user.userEmail =:email ORDER BY user.roleName ASC ");
        
        Query query = getSession().createQuery(builder.toString());
        query.setCacheable(true);
        query.setParameter("email",email);
        
        return query.list();
    }

}
